package com.githup.yfxiaodou.web;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;

@RestController
@RequestMapping("/filecontroller")
public class UploadFileController {

    private Logger log;

    public UploadFileController() {
        this.log = Logger.getLogger(this.getClass());
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(@RequestParam("file")MultipartFile files,HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject json=new JSONObject();
        response.setCharacterEncoding("utf-8");
        String msg = "添加成功";
        log.info("-------------------开始调用上传文件upload接口-------------------");
        try{
            String name = files.getOriginalFilename();
            String path = ResourceUtils.getFile("classpath:application.properties").getParentFile().getAbsolutePath();
            path = path + File.separator+"upload";
            path = path + File.separator + name;
            File uploadFile = new File(path);
            if(!uploadFile.exists()){
                if(!uploadFile.getParentFile().exists()){
                    uploadFile.getParentFile().mkdirs();
                }
                uploadFile.createNewFile();
            }
            files.transferTo(uploadFile);
        }catch(Exception e){
            msg="添加失败";
        }
        log.info("-------------------结束调用上传文件upload接口-------------------");
        json.put("msg", msg);
        return json.toJSONString();
    }

	/*private byte[] inputStreamToByte(InputStream is) throws IOException {
		ByteArrayOutputStream bAOutputStream = new ByteArrayOutputStream();
		int ch;
		while ((ch = is.read()) != -1) {
			bAOutputStream.write(ch);
		}
		byte data[] = bAOutputStream.toByteArray();
		bAOutputStream.close();
		return data;
	}*/

    @RequestMapping(value = "/uploadservlet", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
    protected String uploadServlet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        JSONObject json=new JSONObject();
        response.setCharacterEncoding("utf-8");
        String msg = "添加成功";
        log.info("-------------------开始调用上传文件uploadservlet接口-------------------");
        try {
            if (request instanceof MultipartHttpServletRequest) {
                MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;
                List<MultipartFile> multipartFile = mr.getFiles("file");
                if (null != multipartFile && !multipartFile.isEmpty()) {
                    MultipartFile file = multipartFile.get(0);
                    String name = file.getOriginalFilename();
                    String path = ResourceUtils.getFile("classpath:application.properties").getParentFile().getAbsolutePath();
                    path = path + File.separator+"upload";
                    path = path + File.separator + name;
                    File uploadFile = new File(path);
                    if(!uploadFile.exists()){
                        if(!uploadFile.getParentFile().exists()){
                            uploadFile.getParentFile().mkdirs();
                        }
                        uploadFile.createNewFile();
                    }
                    if(FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(uploadFile))>0)
                    {
                        json.put("path",path);
                    }

                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            msg = "上传失败";
        }
        log.info("-------------------结束调用上传文件uploadservlet接口-------------------");
        json.put("msg", msg);
        return json.toJSONString();
    }
}